package com.corgi;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author zhouhui 登录界面输入
 */
public class LockActivity extends Activity {
	Messenger serviceMessenger = null;
	boolean isBound;
	String packageName = null;
	String password = null;
	int taskId = -1;
	EditText etPassword = null;
	TextView tvInfo = null;

	private ServiceConnection mConnections = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			serviceMessenger = new Messenger(service);
		}

		public void onServiceDisconnected(ComponentName className) {
			serviceMessenger = null;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lock);
		// 密码框
		etPassword = (EditText) findViewById(R.id.etPassword);
		tvInfo = (TextView) findViewById(R.id.tvInfo);

		Intent intent = getIntent();
		packageName = intent.getStringExtra("packagename");
		password = intent.getStringExtra("password");
		taskId = intent.getIntExtra("taskid", 1);
		Log.e("blueberry", "packageName=" + packageName);
		Log.e("blueberry", "password=" + password);
		Log.e("blueberry", "taskId=" + taskId);

		Intent serviceIntent = new Intent(this, LockService.class);
		bindService(serviceIntent, mConnections, 0);
		isBound = true;
	}

	public void onResume() {
		super.onResume();
		etPassword.setText(null);
		tvInfo.setVisibility(View.GONE);
	}

	protected void onStop() {
		super.onStop();
		Log.e("blueberry", "onStop");
		if (isBound) {
			unbindService(mConnections);
			isBound = false;
		}
		finish();
	}

	public void onDestroy() {
		super.onDestroy();
	}

	private void sendMessageToService(int val) {
		Log.e("blueberry", "isBound=" + isBound);
		if (isBound) {
			if (serviceMessenger != null) {
				try {
					Message msg = Message.obtain(null, val, packageName);
					msg.arg1 = taskId;
					serviceMessenger.send(msg);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}
	}

	// 点击ok 按钮
	public void onOK(View v) {
		String pwd = etPassword.getText().toString();
		Log.e("blueberry", "pwd=" + pwd);
		if (pwd != null) {
			if (pwd.equals(password)) {
				SharedPreferences sharedPrefs = PreferenceManager
						.getDefaultSharedPreferences(this);
				sharedPrefs.edit().putBoolean(packageName + "login", true)
						.apply();
				sharedPrefs.edit().commit();
				sendMessageToService(LockService.PWD_SUCCESS);
				finish();
			} else {
				tvInfo.setVisibility(View.VISIBLE);
			}
		}
	}

	// 点击取消按钮
	public void onCancel(View v) {
		sendMessageToService(LockService.PWD_CANCEL);
		finish();
	}
}
